home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / test / tclock.c < prev    next >
C/C++ Source or Header  |  2002-10-24  |  5KB  |  251 lines

  1. /* $Id: tclock.c,v 1.22 2002/06/29 23:34:13 tom Exp $ */
  2.  
  3. #include "test.priv.h"
  4.  
  5. #include <math.h>
  6.  
  7. #if TIME_WITH_SYS_TIME
  8. # include <sys/time.h>
  9. # include <time.h>
  10. #else
  11. # if HAVE_SYS_TIME_H
  12. #  include <sys/time.h>
  13. # else
  14. #  include <time.h>
  15. # endif
  16. #endif
  17.  
  18. /*
  19.   tclock - analog/digital clock for curses.
  20.   If it gives you joy, then
  21.   (a) I'm glad
  22.   (b) you need to get out more :-)
  23.  
  24.   This program is copyright Howard Jones, September 1994
  25.   (ha.jones@ic.ac.uk). It may be freely distributed as
  26.   long as this copyright message remains intact, and any
  27.   modifications are clearly marked as such. [In fact, if
  28.   you modify it, I wouldn't mind the modifications back,
  29.   especially if they add any nice features. A good one
  30.   would be a precalc table for the 60 hand positions, so
  31.   that the floating point stuff can be ditched. As I said,
  32.   it was a 20 hackup minute job.]
  33.  
  34.   COMING SOON: tfishtank. Be the envy of your mac-owning
  35.   colleagues.
  36. */
  37.  
  38. /* To compile: cc -o tclock tclock.c -lcurses -lm */
  39.  
  40. #ifndef PI
  41. #define PI 3.141592654
  42. #endif
  43.  
  44. #define sign(_x) (_x<0?-1:1)
  45.  
  46. #define ASPECT 2.2
  47. #define ROUND(value) ((int)((value) + 0.5))
  48.  
  49. #define A2X(angle,radius) ROUND(ASPECT * radius * sin(angle))
  50. #define A2Y(angle,radius) ROUND(radius * cos(angle))
  51.  
  52. /* Plot a point */
  53. static void
  54. plot(int x, int y, char col)
  55. {
  56.     mvaddch(y, x, (chtype) col);
  57. }
  58.  
  59. /* Draw a diagonal(arbitrary) line using Bresenham's alogrithm. */
  60. static void
  61. dline(int pair, int from_x, int from_y, int x2, int y2, char ch)
  62. {
  63.     int dx, dy;
  64.     int ax, ay;
  65.     int sx, sy;
  66.     int x, y;
  67.     int d;
  68.  
  69.     if (has_colors())
  70.     attrset(COLOR_PAIR(pair));
  71.  
  72.     dx = x2 - from_x;
  73.     dy = y2 - from_y;
  74.  
  75.     ax = abs(dx * 2);
  76.     ay = abs(dy * 2);
  77.  
  78.     sx = sign(dx);
  79.     sy = sign(dy);
  80.  
  81.     x = from_x;
  82.     y = from_y;
  83.  
  84.     if (ax > ay) {
  85.     d = ay - (ax / 2);
  86.  
  87.     while (1) {
  88.         plot(x, y, ch);
  89.         if (x == x2)
  90.         return;
  91.  
  92.         if (d >= 0) {
  93.         y += sy;
  94.         d -= ax;
  95.         }
  96.         x += sx;
  97.         d += ay;
  98.     }
  99.     } else {
  100.     d = ax - (ay / 2);
  101.  
  102.     while (1) {
  103.         plot(x, y, ch);
  104.         if (y == y2)
  105.         return;
  106.  
  107.         if (d >= 0) {
  108.         x += sx;
  109.         d -= ay;
  110.         }
  111.         y += sy;
  112.         d += ax;
  113.     }
  114.     }
  115. }
  116.  
  117. int
  118. main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
  119. {
  120.     int i, cx, cy;
  121.     double cr, mradius, hradius, mangle, hangle;
  122.     double sangle, sradius, hours;
  123.     int hdx, hdy;
  124.     int mdx, mdy;
  125.     int sdx, sdy;
  126.     int ch;
  127.     int lastbeep = -1;
  128.     time_t tim;
  129.     struct tm *t;
  130.     char szChar[10];
  131.     int my_bg = COLOR_BLACK;
  132. #if HAVE_GETTIMEOFDAY
  133.     struct timeval current;
  134.     double fraction = 0.0;
  135. #endif
  136.  
  137.     setlocale(LC_ALL, "");
  138.  
  139.     initscr();
  140.     noecho();
  141.     cbreak();
  142.     nodelay(stdscr, TRUE);
  143.     curs_set(0);
  144.  
  145.     if (has_colors()) {
  146.     start_color();
  147. #if HAVE_USE_DEFAULT_COLORS
  148.     if (use_default_colors() == OK)
  149.         my_bg = -1;
  150. #endif
  151.     init_pair(1, COLOR_RED, my_bg);
  152.     init_pair(2, COLOR_MAGENTA, my_bg);
  153.     init_pair(3, COLOR_GREEN, my_bg);
  154.     }
  155. #ifdef KEY_RESIZE
  156.     keypad(stdscr, TRUE);
  157.   restart:
  158. #endif
  159.     cx = (COLS - 1) / 2;    /* 39 */
  160.     cy = LINES / 2;        /* 12 */
  161.     if (cx / ASPECT < cy)
  162.     cr = cx / ASPECT;
  163.     else
  164.     cr = cy;
  165.     sradius = (5 * cr) / 6;    /* 10 */
  166.     mradius = (3 * cr) / 4;    /* 9 */
  167.     hradius = cr / 2;        /* 6 */
  168.  
  169.     for (i = 0; i < 12; i++) {
  170.     sangle = (i + 1) * (2.0 * PI) / 12.0;
  171.     sdx = A2X(sangle, sradius);
  172.     sdy = A2Y(sangle, sradius);
  173.     sprintf(szChar, "%d", i + 1);
  174.  
  175.     mvaddstr(cy - sdy, cx + sdx, szChar);
  176.     }
  177.  
  178.     mvaddstr(0, 0, "ASCII Clock by Howard Jones (ha.jones@ic.ac.uk),1994");
  179.  
  180.     sradius = (4 * sradius) / 5;
  181.     for (;;) {
  182.     napms(100);
  183.  
  184.     tim = time(0);
  185.     t = localtime(&tim);
  186.  
  187.     hours = (t->tm_hour + (t->tm_min / 60.0));
  188.     if (hours > 12.0)
  189.         hours -= 12.0;
  190.  
  191.     mangle = ((t->tm_min + (t->tm_sec / 60.0)) * (2 * PI) / 60.0);
  192.     mdx = A2X(mangle, mradius);
  193.     mdy = A2Y(mangle, mradius);
  194.  
  195.     hangle = ((hours) * (2.0 * PI) / 12.0);
  196.     hdx = A2X(hangle, hradius);
  197.     hdy = A2Y(hangle, hradius);
  198.  
  199. #if HAVE_GETTIMEOFDAY
  200.     gettimeofday(¤t, 0);
  201.     fraction = (current.tv_usec / 1.0e6);
  202. #endif
  203.     sangle = ((t->tm_sec + fraction) * (2.0 * PI) / 60.0);
  204.     sdx = A2X(sangle, sradius);
  205.     sdy = A2Y(sangle, sradius);
  206.  
  207.     dline(3, cx, cy, cx + mdx, cy - mdy, '#');
  208.  
  209.     attrset(A_REVERSE);
  210.     dline(2, cx, cy, cx + hdx, cy - hdy, '.');
  211.     attroff(A_REVERSE);
  212.  
  213.     if (has_colors())
  214.         attrset(COLOR_PAIR(1));
  215.  
  216.     dline(1, cx, cy, cx + sdx, cy - sdy, 'O');
  217.  
  218.     if (has_colors())
  219.         attrset(COLOR_PAIR(0));
  220.  
  221.     mvaddstr(LINES - 2, 0, ctime(&tim));
  222.     refresh();
  223.     if ((t->tm_sec % 5) == 0
  224.         && t->tm_sec != lastbeep) {
  225.         lastbeep = t->tm_sec;
  226.         beep();
  227.     }
  228.  
  229.     if ((ch = getch()) != ERR) {
  230. #ifdef KEY_RESIZE
  231.         if (ch == KEY_RESIZE) {
  232.         flash();
  233.         erase();
  234.         wrefresh(curscr);
  235.         goto restart;
  236.         }
  237. #endif
  238.         break;
  239.     }
  240.  
  241.     dline(0, cx, cy, cx + hdx, cy - hdy, ' ');
  242.     dline(0, cx, cy, cx + mdx, cy - mdy, ' ');
  243.     dline(0, cx, cy, cx + sdx, cy - sdy, ' ');
  244.  
  245.     }
  246.  
  247.     curs_set(1);
  248.     endwin();
  249.     ExitProgram(EXIT_SUCCESS);
  250. }
  251.